//修改个人中心邮箱验证码
function sendCodeChangeEmail($btn){
    var newEmail = $('#jsChangeEmail').val();
    $('#jsChangeEmailCodeBtn').val("正在发送")
    $.ajax({
        url:"/users/sendCode",
        type:'POST',
        data:JSON.stringify({email:newEmail}),
        contentType:"application/json;charset=utf-8",
        success:function (data) {
            if (data!=null){
                $('#jsChangeEmailCodeBtn').val("发送成功");
            }else{
                $('#jsChangeEmailCodeBtn').val("发送失败")
            }
        }

    });

}
//个人资料邮箱修改
function changeEmailSubmit($btn){
    var email = $('#jsChangeEmail').val();
    var code = $('#jsChangeEmailCode').val();
    $.ajax({
        url:"/users/update/email",
        type:"POST",
        data:JSON.stringify({email:email,code:code}),
        contentType:"application/json;charset=utf-8",
        success:function (data) {
            if (data!=null){
                $('#jsChangeEmailBtn').val("修改邮箱成功");
                setTimeout("window.location.reload(true);",2000)
            }else{
                $('#jsChangeEmailBtn').val("修改失败,请检查验证码是否正确");
            }
        }
    })
}

function showResetPwdDialog($btn) {
    Dml.fun.showDialog('#jsResetDialog', '#jsResetPwdTips');
}

function SubmitResetPwd($btn) {
    var password1 = $('#pwd').val();
    var password = $('#repwd').val();
    alert(password1);
    if (password != password1) {
        Dml.fun.showErrorTips($('#jsResetPwdTips'),"两次密码输入不一致")
    } else {
        alert("222");
            $.ajax({
                url:"/users/update/pwd",
                type:"POST",
                contentType:"application/json;charset=utf-8",
                data:JSON.stringify({password: password1}),
                success:function (data) {
                    if (data != null) {
                        Dml.fun.showErrorTips($('#jsResetPwdTips'),"修改成功,5秒后重新登录")
                        setTimeout("window.location.href='/login';",5000);
                    } else {
                        Dml.fun.showErrorTips($('#jsResetPwdTips'),"修改失败!")
                    }
                }

            });
    }
}
    //个人资料头像
    $('.js-img-up').uploadPreview({ Img: ".js-img-show", Width: 94, Height: 94 ,Callback:function(){
        $('#jsAvatarForm').submit();
    }});


    $('.changeemai_btn').click(function(){
        Dml.fun.showDialog('#jsChangeEmailDialog', '#jsChangePhoneTips' ,'jsChangeEmailTips');
    });
    $('#jsChangeEmailCodeBtn').on('click', function(){
        sendCodeChangeEmail($(this));
    });
    $('#jsChangeEmailBtn').on('click', function(){
        changeEmailSubmit($(this));
    });
    $('#jsUserResetPwd').on('click', function(){
        showResetPwdDialog($(this));
    });
    $('#jsResetPwdBtn').on('click',function () {
        SubmitResetPwd($(this))
    });


    //input获得焦点样式
    $('.perinform input[type=text]').focus(function(){
        $(this).parent('li').addClass('focus');
    });
    $('.perinform input[type=text]').blur(function(){
        $(this).parent('li').removeClass('focus');
    });

    laydate({
        elem: '#birth_day',
        format: 'YYYY-MM-DD',
        max: laydate.now()
    });

    verify(
        [
            {id: '#nick_name', tips: Dml.Msg.epNickName, require: true}
        ]
    );
    //保存个人资料
    $('#jsEditUserBtn').on('click', function(){
        var nike_name = $('#nick_name').val();
        var birthday = $('#birth_day').val();
        var obj=document.getElementsByName("gender")
        var chkObjs;
        for (var i=0;i<obj.length;i++){
            if(obj[i].checked){
                chkObjs=obj[i].value;
            }
        }
        var gender = chkObjs;
        var address = $('#address').val();
        var mobile = $('#mobile').val();
        $.ajax({
            url:"/users/info",
            type:"POST",
            data:JSON.stringify({nike_name:nike_name,birthday:birthday,gender:gender,address:address,mobile:mobile}),
            contentType:"application/json;charset=UTF-8",
            success:function (data) {
                if (data!=null){
                    alert("修改成功");
                    window.location.reload(true);
                }
                else{
                    alert("修改失败")
                }
            }
        })
    })
